<!--
  签订DNA
  @author YangMiao 1939724848@qq.com
  @date 2022/3/28 15:30
  @version 1.0.0
-->
<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm" @submit.native.prevent>
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('申请标题')" prop="input5">
            <el-input v-model="form.input5" placeholder="请输入标题" show-word-limit :maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('是否是标准版')" prop="dictselect">
            <dict-select v-model="form.dictselect" dictionary="sf_int" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('关键注意事项')">
            <el-input v-model="form.input4" placeholder="请输入关键注意事项" type="textarea" show-word-limit :maxlength="200" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 附件列表 -->
      <FileTable :list.sync="form.fileList" data-type="sign_nda" :btn-show="!otherBtnHide" />
    </el-form>
  </div>
</template>

<script>
import { saveNdaOne, getNdaOne } from '@/api/sass/yueyin/deal/signDNA/ndaOne'
import { getDealName } from '@/api/base/deal/dealBase'

export default {
  data() {
    return {
      // 表单参数
      form: {
        input5: undefined,
        dictselect: undefined,
        input4: undefined,
        fileList: undefined
      },
      // 表单校验
      rules: {
        input5: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        dictselect: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }]
      },
      // 遮罩层
      loading: false,
      // 操作按钮默认不展示
      buttonShow: true,
      // 是否隐藏保存按钮
      otherBtnHide: false
    }
  },
  created() {
    this.$bus.$emit('async_done')
  },
  methods: {
    // 初始化数据
    async init(data) {
      console.log('asd', data)
      this.loading = true
      if (data != null && data.id !== undefined) {
        console.log('asd')
        getNdaOne(data.id).then(response => {
          this.form = response.data
          this.loading = false
        })
      } else {
        // 初始化数据
        /* getByWfNodeId(data.wfNodeId).then(async response => {
          if(response.data !== null){
            this.form = response.data
          }
          data.fileList[0].name = ''
          data.fileList[0].url = ''
          data.fileList[0].path = ''
          this.form.fileList = data.fileList
          // 根据项目id查询项目名称
          this.form.input5 = await getDealName(this.$route.query.id) + '-签订NDA'
          this.loading = false
        })*/
        this.reset()
        // 默认带出流程中必须要传的附件
        if (data !== null) {
          this.form.fileList = data.fileList
        }
        this.form.input5 = await getDealName(this.$route.query.id) + '-签订NDA'
        this.loading = false
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        dealId: this.$route.query.id,
        input5: undefined,
        dictselect: undefined,
        input4: undefined
      }
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.formOpen = false
      this.$parent.$parent.reset()
    },
    // 关闭form页面
    closeForm(type) {
      // 关闭form页面遮罩层
      this.loading = false
      if (type !== 'actSave') {
        // 关闭form页面
        this.$parent.$parent.formOpen = false
        // 刷新list页面
        this.$parent.$parent.getList()
      }
    },
    /** 提交按钮 */
    submitForm: function(type) {
      return new Promise((resolve, reject) => {
        this.$refs['form'].validate(valid => {
          // 验证数据
          if (valid) {
            // 弹出遮罩层
            this.loading = true
            // 保存数据
            saveNdaOne(this.form).then(response => {
              console.log('type', type)
              console.log('保存结果', response)
              if (response.code === 200) {
                // 点击的按钮是保存并启动流程
                if (type === 'process') {
                  console.log('启动流程')
                  // 设置数据id方式启动流程后取消并再次启动会创建多条记录
                  this.form.id = response.data.id
                  // 启动流程
                  this.startProcess(response.data.id)
                  // 点击的仅保存
                } else {
                  console.log('仅保存')
                  this.msgSuccess(this.$t('保存成功'))
                  // 关闭form页面
                  this.closeForm(type)
                }
                this.form.id = response.data.id
                console.log('返回id', response.data.id)
                resolve(response.data.id)
              } else {
                this.msgError(response.msg)
                this.loading = false
                reject(response)
              }
            }).catch(() => {
              this.loading = false
            })
          } else {
            reject()
            this.locationError(document)
          }
        })
      })
    },
    // 启动流程
    startProcess: async function(dataId) {
      const dealName = await getDealName(this.$route.query.id)
      console.log(dealName)
      this.loading = false
      // 启动流程时候传入的参数
      const processData = {}
      // 待办标题
      processData.title = this.form.input5
      // 业务表名
      processData.dataTable = 'nda_one'
      console.log('业务id', dataId)
      // 业务ID
      processData.dataId = dataId
      // 业务表单地址(待办页面需要打开的页面)
      processData.formPath = '@/views/saas/yueyin/deal/signDNA'
      processData.viewPath = '@/views/saas/yueyin/deal/signDNA'
      // 菜单标识(根据菜单标识判断的走哪个流程)
      processData.menuKey = 'nda_one'
      this.startProcessByMenu(processData, () => {
        this.form.actStatus = 'D'
        this.msgSuccess(this.$t('数据保存成功,流程启动成功'))
        this.closeForm()
      })
    }

  }

}
</script>

<style scoped>

</style>
